package site.yunnong.atvris.common.api;

import lombok.Data;

import java.util.List;

/**
 * 分页数据封装类
 * @date 2021年4月10日14点41分
 */
@Data
public class Page<T> {
    /**
     * 当前页码
     */
    private Integer currentPage;
    /**
     * 每页数量
     */
    private Integer pageSize;
    /**
     * 总页数
     */
    private Integer totalPage;
    /**
     * 总条数
     */
    private Long total;
    /**
     * 分页数据
     */
    private List<T> list;

    private Integer isMore;

    private Integer startIndex;
    
    public Page() {
        
    }

    public Page(Integer currentPage, Integer pageSize, Long total, List<T> list) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
        this.totalPage = (this.total.intValue() + this.pageSize - 1) / this.pageSize;
        this.startIndex = this.pageSize * (this.currentPage - 1);
        this.isMore = this.currentPage < this.totalPage ? 1 : 0;
    }
    
    public <T> Page<T> restPage(org.springframework.data.domain.Page<T> page) {
        Page<T> result = new Page<>();
        // jpa 页码从0开始
        result.setCurrentPage(page.getNumber() + 1);
        result.setPageSize(page.getSize());
        result.setTotal(page.getTotalElements());
        result.setList(page.getContent());
        result.setTotalPage(page.getTotalPages());
        result.setStartIndex(page.getSize() * page.getNumber());
        result.setIsMore(page.getNumber() + 1 < page.getTotalPages() ? 1 : 0);
        return result;
    } 
}
